package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.model.AddressModel;
import com.app.model.CategoryModel;
import com.app.model.IntegralOrderModel;
import com.app.model.LotteryRecord;
import com.app.service.AddressService;
import com.app.service.CategoryService;
import com.app.service.ILotteryRecordService;
import com.app.service.IntegralOrderService;
import com.app.utils.CheckDeliveryUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Configuration
@EnableScheduling
public class TimedTasksController {
    @Autowired
    private IntegralOrderService integralOrderService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private ILotteryRecordService iLotteryRecordService;

    // 0 0 3 * * ?
//    @Scheduled(cron = "0 0 3 * * ?") // 每天凌晨3点运行
//    public void queryInvoiceStatus() {
//        try {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            QueryWrapper<IntegralOrderModel> integralOrderModelQueryWrapper = new QueryWrapper<>();
//            integralOrderModelQueryWrapper.eq("status", 2);
//            List<IntegralOrderModel> orderModelList = integralOrderService.list(integralOrderModelQueryWrapper);
//            for (IntegralOrderModel integralOrderModel : orderModelList) {
//                if (integralOrderModel.getStatus() == 2) {
//                    if (integralOrderModel.getExpressCompany() != null && integralOrderModel.getTrackingNumber() != null) {
//                        CategoryModel categoryModel = categoryService.getById(integralOrderModel.getExpressCompany());
//                        if (categoryModel != null) {
//                            if ("顺丰".equals(categoryModel.getName()) || "丰网速运".equals(categoryModel.getName())) {
//                                AddressModel addressModel = addressService.getById(integralOrderModel.getAddressId());
//                                if (addressModel != null) {
//                                    String delivery = CheckDeliveryUtil.getCheckDelivery(integralOrderModel.getTrackingNumber() + ":" + addressModel.getPhone().substring(addressModel.getPhone().length() - 4), categoryModel.getReferred());
//                                    JSONObject jsonObject = JSON.parseObject(delivery);
//                                    if (jsonObject.containsKey("deliverystatus")) {
//                                        if (jsonObject.get("deliverystatus").equals(3)) {
//                                            if (jsonObject.containsKey("updateTime")) {
//                                                Date date1 = sdf.parse(jsonObject.get("updateTime").toString());
//                                                if (isOutTime(date1)) {
//                                                    integralOrderModel.setStatus(3);
//                                                }
//                                            }
//                                        }
//                                    }
//                                    integralOrderModel.setMessage(delivery);
//                                    integralOrderService.updateById(integralOrderModel);
//                                }
//                            } else {
//                                String delivery = CheckDeliveryUtil.getCheckDelivery(integralOrderModel.getTrackingNumber(), categoryModel.getReferred());
//                                JSONObject jsonObject = JSON.parseObject(delivery);
//                                if (jsonObject.containsKey("deliverystatus")) {
//                                    if (jsonObject.get("deliverystatus").equals(3)) {
//                                        if (jsonObject.containsKey("updateTime")) {
//                                            Date date1 = sdf.parse(jsonObject.get("updateTime").toString());
//                                            if (isOutTime(date1)) {
//                                                integralOrderModel.setStatus(3);
//                                            }
//                                        }
//                                    }
//                                }
//                                integralOrderModel.setMessage(delivery);
//                                integralOrderService.updateById(integralOrderModel);
//                            }
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            log.error("定时任务出错", e);
//        }
//    }
//
//    public static boolean isOutTime(Date data) throws Exception {
//        //24小时
//        long house = 60 * 60 * 24;
//        //系统当前时间,
//        long currentTime = System.currentTimeMillis() / 1000;
//        //业务时间
//        long endTime = data.getTime() / 1000;
//        if ((endTime + house) < currentTime) {
//            log.info("超过了24小时");
//            return true;
//        } else {
//            log.info("没有超过");
//            return false;
//        }
//    }
//
//    @Scheduled(cron = "0 0 3 * * ?") // 每天凌晨3点运行
//    public void queryStatus() {
//        try {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            QueryWrapper<LotteryRecord> lotteryRecordQueryWrapper = new QueryWrapper<>();
//            lotteryRecordQueryWrapper.eq("type", 1);
//            lotteryRecordQueryWrapper.eq("isSend", 1);
//            List<LotteryRecord> recordList = iLotteryRecordService.list(lotteryRecordQueryWrapper);
//            for (LotteryRecord lotteryRecord : recordList) {
//                if (lotteryRecord.getExpressCompany() != null && lotteryRecord.getTrackingNumber() != null) {
//                    CategoryModel categoryModel = categoryService.getById(lotteryRecord.getExpressCompany());
//                    if (categoryModel != null) {
//                        if ("顺丰".equals(categoryModel.getName()) || "丰网速运".equals(categoryModel.getName())) {
//                            AddressModel addressModel = addressService.getById(lotteryRecord.getAddressId());
//                            if (addressModel != null) {
//                                String delivery = CheckDeliveryUtil.getCheckDelivery(lotteryRecord.getTrackingNumber() + ":" + addressModel.getPhone().substring(addressModel.getPhone().length() - 4), categoryModel.getReferred());
//                                JSONObject jsonObject = JSON.parseObject(delivery);
//                                if (jsonObject.containsKey("deliverystatus")) {
//                                    if (jsonObject.get("deliverystatus").equals(3)) {
//                                        if (jsonObject.containsKey("updateTime")) {
//                                            Date date1 = sdf.parse(jsonObject.get("updateTime").toString());
//                                            if (isOutTime(date1)) {
//                                                lotteryRecord.setIsSend(2);
//                                            }
//                                        }
//                                    }
//                                }
//                                lotteryRecord.setMessage(delivery);
//                                iLotteryRecordService.updateById(lotteryRecord);
//                            }
//                        } else {
//                            String delivery = CheckDeliveryUtil.getCheckDelivery(lotteryRecord.getTrackingNumber(), categoryModel.getReferred());
//                            JSONObject jsonObject = JSON.parseObject(delivery);
//                            if (jsonObject.containsKey("deliverystatus")) {
//                                if (jsonObject.get("deliverystatus").equals(3)) {
//                                    if (jsonObject.containsKey("updateTime")) {
//                                        Date date1 = sdf.parse(jsonObject.get("updateTime").toString());
//                                        if (isOutTime(date1)) {
//                                            lotteryRecord.setIsSend(2);
//                                        }
//                                    }
//                                }
//                            }
//                            lotteryRecord.setMessage(delivery);
//                            iLotteryRecordService.updateById(lotteryRecord);
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            log.error("定时任务出错", e);
//        }
//    }
}
